<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="快速微调" :visible.sync="dialogVisible" @close="resetForm('subRef')" width="400px">
        <el-form label-position="top" size="medium" :model="subForm" :hide-required-asterisk='true' ref="subRef" :rules="subRules" class="dialog_form">
            <el-form-item label="微调素材" prop="">
                <div class="info_box">{{title}}</div>
            </el-form-item>
            <el-form-item label="业务场景" prop="id" style="margin-bottom:0" :rules="{ required: true, message: '请选择业务场景', trigger: ['blur', 'change']}">
                <el-select v-model="subForm" filterable placeholder="请选择业务场景" value-key='id' style="width:100%" @change="selectBusiness">
                    <el-option :label="item.name" :value="item" v-for="item in channelList" :key="item.id"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div class="bess_tips mt_16">1.需要在对应平台下添加相同项目才可修改。</div>
        <div class="bess_tips">2.调整后将产生新的素材数据，原素材不会覆盖。</div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('subRef')" class="el-button-width" size="small">取 消</el-button>
            <el-button type="primary" @click="submitForm('subRef')" class="el-button-width" size="small" :disabled='disabled' :loading="disabled">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import filters from '@/assets/js/filters.js'
export default {
    name: "",
    data() {
        return {
            disabled: false,
            dialogVisible: false,
            channelList:[],
            id:null,
            title:'',
            subForm: {},
        };
    },

    created() {
        
    },
    methods: {
        //获取品牌列表
        async getBusunessList() {
            const { data: res } = await this.$http.post(
                "/adminapi/material/microAdjustSelect",
                {
                    id:this.id
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                let arr = []
                for(let i in res.data){
                    arr.push({
                        id:res.data[i].id,
                        channel:res.data[i].channel,
                        business_scene:res.data[i].business_scene,
                        brand_id:res.data[i].brand_id,
                        product_id:res.data[i].product_id,
                        name:filters.chanelName2(res.data[i].channel)+'-'+filters.sceneFilter(res.data[i].business_scene)
                    })
                }
                this.channelList = arr
            }
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    let queryInfo = {
                        id:this.id,
                        channel:this.subForm.channel,
                        business_scene:this.subForm.business_scene,
                        brand_id:this.subForm.brand_id,
                        product_id:this.subForm.product_id,
                    }
                    this.loading = true
                    const { data: res } = await this.$http.post(
                        "/adminapi/material/microAdjust",
                        queryInfo
                    );
                    if (res.code !== 200) {
                        this.loading = false;
                        this.$message.error(res.msg);
                        return;
                    }else{
                        this.$message({
                            type: "success",
                            message: "保存成功!",
                        });
                        this.resetForm(formName)
                        this.$emit("openTrimTask")
                    }
                } else {
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.dialogVisible = false;
            // this.$refs[formName].resetFields();
            this.subForm = {}
        },
    },
};
</script>
<style scoped>
.info_box {
    font-size: 14px;
    color: #17233d;
    line-height: 20px;
    padding: 10px;
    background-color: rgba(165, 177, 209, 0.08);
    border-radius: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.bess_tips{
    font-size: 12px;
    color: #B4B7BD;
    margin-top: 8px;
}
.mt_16{
    margin-top: 16px;
}
</style>

